package com.fzebu.fastboot.docs;

import org.apache.commons.io.FileUtils;
import weaver.docs.webservices.DocAttachment;
import weaver.docs.webservices.DocInfo;
import weaver.docs.webservices.DocServiceImpl;
import weaver.hrm.User;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Jack
 * @version 1.0.0
 * @create 2020/2/21 13:38
 */
public class DocUtil {

     public static int saveAttachment(String fileName) throws IOException {
          return saveAttachment(new File(fileName));
     }

     public static int saveAttachment(File file) throws IOException {
          FileInputStream inputStream = FileUtils.openInputStream(file);
          return saveAttachment(inputStream, file.getName());
     }

     public static int saveAttachment(InputStream inputStream, String name) {
          return com.engine.odoc.util.DocUtil.saveImageFile(inputStream, name, name);
     }

     public static int saveAttachment2Docs(String filePath, String docName, int secId) throws Exception {
          return saveAttachment2Docs(filePath, docName, secId, 1);
     }

     public static int saveAttachment2Docs(String filePath, String docName, int secId, int creator) throws Exception {
          return saveAttachment2Docs(new File(filePath), docName, secId, creator);
     }

     public static int saveAttachment2Docs(String filePath, int secId) throws Exception {
          return saveAttachment2Docs(new File(filePath), secId);
     }

     public static int saveAttachment2Docs(File file, int secId) throws Exception {
          return saveAttachment2Docs(file, secId, 1);
     }

     public static int saveAttachment2Docs(File file, int secId, int creator) throws Exception {
          return saveAttachment2Docs(file, file.getName(), secId, creator);
     }

     public static int saveAttachment2Docs(File file, String docName, int secId, int creator) throws Exception {
          if (file == null || secId <= 0 || creator <= 0) {
               return -1;
          }
          DocInfo docInfo = new DocInfo();
          // 设置文档目录ID
          docInfo.setSeccategory(secId);
          docInfo.setMaincategory(secId);
          docInfo.setSubcategory(secId);
          // 设置文档名称
          docInfo.setDocSubject(docName);
          // 设置附件
          DocAttachment attachment = new DocAttachment();
          attachment.setFilename(file.getName());
          attachment.setFilerealpath(file.getAbsolutePath());
          docInfo.setAttachments(new DocAttachment[]{attachment});
          return saveDocs(docInfo, User.getUser(creator, 0));
     }

     public static int saveDocs(DocInfo docInfo, User user) throws Exception {
          DocServiceImpl service = new DocServiceImpl();
          return service.createDocByUser(docInfo, user);
     }
}
